package com.jy.rhin.domain.document.filter.impl;

import com.jy.rhin.domain.document.filter.DocumentFilter;
import com.jy.rhin.domain.document.filter.DocumentFilterDescEnum;
import com.jy.rhin.domain.exception.RhinInfraErrorType;
import com.jy.rhin.infra.uitl.Asserts;
import com.jy.rhin.sdk.dto.document.DocumentContentDto;
import com.jy.rhin.sdk.dto.document.DocumentEntryDto;
import com.jy.rhin.sdk.dto.document.DocumentSubmissionSetDto;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2022/6/2
 * @Description
 */
@Service
public class DocumentFilter4ContentIdInEntryList implements DocumentFilter {
    @Override
    public boolean shouldFilter(DocumentSubmissionSetDto submissionSet, List<DocumentContentDto> contentList) {
        return !CollectionUtils.isEmpty(submissionSet.getEntries()) && !CollectionUtils.isEmpty(contentList);
    }

    /**
     * documents节点下所有的id，在提交集/entry节点下必定存在
     *
     * @param submissionSet
     * @param contentList
     */
    @Override
    public void doFilter(DocumentSubmissionSetDto submissionSet, List<DocumentContentDto> contentList) {
        Set<String> dtoList = submissionSet.getEntries().stream().map(DocumentEntryDto::getId).collect(Collectors.toSet());
        List<String> contentIdList = contentList.stream().map(DocumentContentDto::getId).collect(Collectors.toList());
        Asserts.isTrue(dtoList.containsAll(contentIdList), RhinInfraErrorType.DOCUMENT_ID_NOT_FOUND);
    }

    @Override
    public DocumentFilterDescEnum filterDesc() {
        return DocumentFilterDescEnum.CONTENT_ID_IN_ENTRY_LIST;
    }

}
